// pages/rescue/apply/apply.js
// 引入SDK核心类
var QQMapWX = require('../../../libs/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    latitude: "",
    longitude: "",
    mapH:0,
    modal8: false,
    markers:[],
    polyline:[],
    car:null,
    shop:null,
    range:0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    var data =  JSON.parse(decodeURIComponent(options.data));
    this.init(data);

    //创建节点选择器
    var query = wx.createSelectorQuery();
    //选择id
    query.select('#scrollView').boundingClientRect()
    wx.getSystemInfo({
      success: function(res) {
        query.exec(function (res2) {
          //res就是 所有标签为mjltest的元素的信息 的数组
          //console.log(res);
          //取高度
          console.log(res2[0].height);
          var bottomH = res2[0].height;
          that.setData({
           mapH:res.windowHeight - bottomH
          })
        })
      }
    })
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: '6KMBZ-65KR3-RWN3M-Y3MIB-4OFGF-HDFK6'
    });
    that.currentLocation();
  },
  init:function(data){
    this.setData({
      latitude: data.latitude,
      longitude: data.longitude,
      car:data.car,
      shop:data.shop,
      range:data.range
    });
  },
  currentLocation:function(){
    //当前位置
    const that = this;
    var points = [];
    points.push({
      longitude: that.data.longitude,
      latitude: that.data.latitude
    });
    points.push({
      longitude:that.data.shop.longitude ,
      latitude: that.data.shop.latitude
    });
        var marker = {
          id: 1,
          title:'3333333',
          latitude: that.data.shop.latitude,
          longitude: that.data.shop.longitude,
          callout:{
            content:'门店据您当前位置'+that.data.range+'公里\n请耐心等待',
            color: '#333333',
            fontSize: 12,
            borderRadius: 10,
            borderWidth: 1,
            borderColor: "#CCC",
            padding:8,
            display: 'ALWAYS',
          }
        };
        var markers = new Array();
        markers.push(marker);
        var pl = [{
          points: points,
          color: "#0066FF",
          width: 2,
          dottedLine: false
        }];
    that.setData({
        polyline:pl,
        markers:markers,
        latitude: that.data.latitude,
        longitude: that.data.longitude
     })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selectCar:function(){
    this.setData({
      modal8: true
    })
  },
  hide8() {
    this.setData({
      modal8: false
    })
  },
  apply:function(){
    wx.navigateBack({
      complete: (res) => {},
    })
  },
  go:function(e){
    const nickname     = this.data.shop.nickname
    const shop_address = this.data.shop.shop_address
    const latitude     = Number(this.data.latitude)
    const longitude    = Number(this.data.longitude)
    wx.openLocation({
      name   : nickname,
      address: shop_address,
      latitude,
      longitude,
      scale: 18
    })
  }
})